package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.dto.BrandDto;
import com.huanghuai.retail.entity.Factory;
import com.huanghuai.retail.entity.PBrand;
import com.huanghuai.retail.mapper.BrandMapper;
import com.huanghuai.retail.service.BrandService;
import com.huanghuai.retail.service.FactoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, PBrand> implements BrandService {
    @Autowired
    private BrandService brandService;
    @Autowired
    private FactoryService factoryService;


    /**
     * 根据brand的名称进行模糊查询，返回对应ids
     * @param name
     * @return
     */
    @Override
    public List<Long> getIdsByName(String name) {
        LambdaQueryWrapper<PBrand> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(name==null){
            return null;
        }
        lambdaQueryWrapper.like(name!=null, PBrand::getName, name);
        List<PBrand> list = brandService.list(lambdaQueryWrapper);
        List<Long> ids = new ArrayList<>();
        for (int i = 0; i < list.size(); i ++) {
            ids.add(list.get(i).getId());
        }
        return ids;
    }

    //品牌dto数据处理
    @Override
    public BrandDto solveDto(PBrand brand) {

        BrandDto brandDto = new BrandDto();
        BeanUtils.copyProperties(brand, brandDto);
        //获取工厂名称
        Long factoryId = brand.getFactoryId();
        LambdaQueryWrapper<Factory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Factory::getId,factoryId);
        Factory factory = factoryService.getOne(queryWrapper);
        if(factory!=null)
        brandDto.setFactoryName(factory.getName());
        return brandDto;
    }


}
